package com.wanmait.zaisousuo.controller;

import com.wanmait.zaisousuo.pojo.Answer;
import com.wanmait.zaisousuo.service.AnswerService;
import com.wanmait.zaisousuo.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/backstage/answer")
public class AnswerController {
    @Resource
    private AnswerService answerService;
    @Resource
    private QuestionService questionService;

    @RequestMapping("list")
    public void answer(Integer questionId, Model model){
        List<Answer> answers = answerService.findByQuestion(questionId);
        model.addAttribute("answers",answers);
    }
    @RequestMapping("show")
    public void show(Integer id,Model model){
        Answer answer = answerService.findById(id);
        model.addAttribute("answer",answer);
    }

    @RequestMapping("updateDisabled")
    @ResponseBody
    public void updateDisabled(Integer id,boolean disabled){
        questionService.clearCache();//清除问答的cache
        answerService.updateDisabled(id,disabled);
    }

    @RequestMapping("delete")
    @ResponseBody
    public void delete(Integer id){
        questionService.clearCache();//清除问答的cache
        answerService.delete(id);
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public void deleteAll(String ids){
        questionService.clearCache();//清除问答的cache
        answerService.delete(ids);
    }

    @RequestMapping("recoverAll")
    @ResponseBody
    public void recoverAll(String ids){
        questionService.clearCache();//清除问答的cache
        answerService.recover(ids);
    }
}
